package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.ExamPaperPersonResultMapper;
import com.zhijian.medical.entity.po.ExamPaperPersonResult;
import com.zhijian.medical.entity.vo.request.ExamPaperPersonResultSearchVo;
import com.zhijian.medical.entity.vo.response.ExamPaperPersonResultVo;
import com.zhijian.medical.service.ExamPaperPersonResultService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-试卷人员结果表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-18 18:26:49
 */
@Slf4j
@Service
public class ExamPaperPersonResultServiceImpl implements ExamPaperPersonResultService {

    @Autowired
    private ExamPaperPersonResultMapper mapper;

    @Override
    public ExamPaperPersonResult selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(ExamPaperPersonResult examPaperPersonResult) {
        Date now = new Date();
        examPaperPersonResult.setCreateTime(now);
        examPaperPersonResult.setUpdateTime(now);
        return mapper.insert(examPaperPersonResult) > 0 ? examPaperPersonResult.getId() : 0L;
    }

    @Override
    public boolean update(ExamPaperPersonResult examPaperPersonResult) {
        Date now = new Date();
        examPaperPersonResult.setUpdateTime(now);
        return mapper.updateByPrimaryKey(examPaperPersonResult) != 0;
    }

    @Override
    public Integer count(ExamPaperPersonResultSearchVo examPaperPersonResultSearchVo) {
        return mapper.count(examPaperPersonResultSearchVo);
    }

    @Override
    public List<ExamPaperPersonResultVo> selectList(ExamPaperPersonResultSearchVo examPaperPersonResultSearchVo) {
        return mapper.selectList(examPaperPersonResultSearchVo);
    }

    @Override
    public Page<ExamPaperPersonResultVo> selectPage(ExamPaperPersonResultSearchVo examPaperPersonResultSearchVo) {
        //先直接查总数
        int count = count(examPaperPersonResultSearchVo);
        //分页
        Page<ExamPaperPersonResultVo> page = new Page<>(true, count, examPaperPersonResultSearchVo.getPageNum(), examPaperPersonResultSearchVo.getPageSize());
        examPaperPersonResultSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExamPaperPersonResultVo> responseVos = this.selectList(examPaperPersonResultSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean deleteByExamPaperId(Long examPaperId) {
        return mapper.deleteByExamPaperId(examPaperId);
    }

    @Override
    public Integer selectRightCountByExamPaperIdAndUserId(Long examPaperPersonId, Long userId) {
        return mapper.selectRightCountByExamPaperIdAndUserId(examPaperPersonId, userId);
    }

}